<?php
ini_set("mssql.textlimit",2147483647);
ini_set("mssql.textsize",2147483647);
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of EntregaEnf
 *
 * @author amartinez
 */
class EntregaEnf {
    private $fechar;
    private $usuario;
    private $tablero;
    private $paciente;
    private $fechaIng;
    private $diagnostico;
    private $segregacion;
    private $observaciones;
    private $hora;
            
    function __construct($fechar, $usuario, $tablero, $paciente, $fechaIng, $diagnostico, $segregacion, $observaciones, $hora){
        $this->fechar = $fechar;
        $this->usuario = $usuario;
        $this->tablero = $tablero;
        $this->paciente = $paciente;
        $this->fechaIng = $fechaIng;
        $this->diagnostico = $diagnostico;
        $this->segregacion = $segregacion;
        $this->observaciones = $observaciones;
        $this->hora = $hora;
    }

    public function getFechar(){
        return $this->fechar;
    }
    public function setFechar($fechar){
        $this->fechar = $fechar;
    }
    public function getUsuario(){
        return $this->usuario;
    }
    public function setUsuario($usuario){
        $this->usuario = $usuario;
    }
    public function getTablero(){
        return $this->tablero;
    }
    public function setTablero($tablero){
        $this->tablero = $tablero;
    }
    public function getPaciente(){
        return $this->paciente;
    }
    public function setPaciente($paciente){
        $this->paciente = $paciente;
    }
    public function getFechaIng(){
        return $this->fechaIng;
    }
    public function setFechaIng($fechaIng){
        $this->fechaIng = $fechaIng;
    }
    public function getDiagnostico(){
        return $this->diagnostico;
    }
    public function setDiagnostico($diagnostico){
        $this->diagnostico = $diagnostico;
    }
    public function getSegregacion(){
        return $this->segregacion;
    }
    public function setSegregacion($segregacion){
        $this->segregacion = $segregacion;
    }
    public function getObservaciones(){
        return $this->observaciones;
    }
    public function setObservaciones($observaciones){
        $this->observaciones = $observaciones;
    }
    public function getHora(){
        return $this->hora;
    }
    public function setHora($hora){
        $this->hora = $hora;
    }
            
    function listaTablerosEnh($codser){
         include_once '../../../controlador/php/conexion/Conexion.php';
        
        $con = new Conexion();
         
        $listaTableros = "SELECT cast(t.enfactual as text) as enfact, 
                                 cast(t.tratamiento as text)as tratamiento,
                                 nes.n,t.oid as oidst,t.oid,t.cama,servicio,t. paciente,HCACODIGO,dg10.NOMPAC,dg10.AINCONSEC,dg10.GDENOMBRE, 
                                 auxiliar,medico,especialidad,pendientes,t.fecha,cast(diagnostico as text) as diagnostico,cast(observaciones as text) as observaciones,
                                 especialidadoid,clasificacion,atendido,enobservacion,t.usu_oid,fecharegis,dg10.EDAD,estado,entfecingr,enthora,cast(entdiaenfe as text) as entdiaenfe,entsegrega,cast(entobserva as text) as entobserva,
                                 entrevisado.oid as revisado,entrevisado.revestado,ALERGIAS._7, ALERGIAS._7cual
                         FROM setablero as t
                         LEFT OUTER JOIN ".$con->getReal()."..HPNDEFCAM on t.cama = HPNDEFCAM.OID 
                         left outer join enturenfer on t.paciente = enturenfer.paciente 
                         LEFT OUTER JOIN entrevisado ON t.oid = entrevisado.setablero
                         outer apply(select RTRIM(PACPRINOM+' '+PACSEGNOM+' '+PACPRIAPE+' '+PACSEGAPE)AS NOMPAC, GDENOMBRE,AINCONSEC,SUBSTRING(CONVERT (VARCHAR(10),AINFECING,103),1,10)+' '+CONVERT(VARCHAR(5), AINFECING, 114) AS FECHA,
                         CAST(DATEDIFF(DD, GENPACIEN.GPAFECNAC, GETDATE()) / 365.25 AS INT) AS EDAD
                                     from ".$con->getReal()."..GENPACIEN left outer join ".$con->getReal()."..ADNINGRESO on GENPACIEN.OID = ADNINGRESO.GENPACIEN
                                           INNER JOIN ".$con->getReal()."..GENDETCON AS GE ON ADNINGRESO.GENDETCON=GE.OID
                                     WHERE AINESTADO=0  and PACNUMDOC = t.paciente)as dg10
                         outer apply (select count(*) as n
                                 from setespecialidad
                                 where setespecialidad.setablero = t.oid and setespecialidad.paciente = t.paciente)as nes
                                 OUTER APPLY(SELECT TOP 1 HCFECFOL, AINCONSEC, PACNUMDOC, PACPRINOM, PACSEGNOM, PACPRIAPE, PACSEGAPE, 
                                                CONVERT(NVARCHAR(5),HCCM09N16) AS '_7', CONVERT(NVARCHAR(500),HCCM04N29) AS '_7cual' 
                                                FROM ".$con->getReal()."..HCMHODETE 
                                                INNER JOIN ".$con->getReal()."..HCNFOLIO ON HCMHODETE.HCNFOLIO = HCNFOLIO.OID 
                                                INNER JOIN ".$con->getReal()."..HCNTIPHIS ON HCNFOLIO.HCNTIPHIS = HCNTIPHIS.OID 
                                                INNER JOIN ".$con->getReal()."..ADNINGRESO ON HCNFOLIO.ADNINGRESO = ADNINGRESO.OID 
                                                INNER JOIN ".$con->getReal()."..GENPACIEN ON ADNINGRESO.GENPACIEN = GENPACIEN.OID 
                                            WHERE HCCODIGO='HODETE' AND PACNUMDOC = t.paciente
                    ORDER BY HCFECFOL DESC)AS ALERGIAS
                         WHERE servicio = '".$codser."'";
        $res = $con->query($listaTableros);
        
        while($res2 = mssql_fetch_array($res)){
            $listat[] = array_map(utf8_encode,$res2);
        }
        
        print_r(json_encode($listat));
        
        $con->cerrarConexion();
        
    }
    function insertardatos(){
        session_start();
        include '../../../controlador/php/conexion/Conexion.php';
        $con = new Conexion();
        
        $count = "select COUNT(*) as n from enturenfer where setablero = '".$this->tablero."' and paciente = '".$this->paciente."' ";
        $res = $con->query($count);
        
        while($res2 = mssql_fetch_array($res)){
           $n = $res2['n'];
        }if($n == 0 ){
            $insert = "insert into enturenfer(entfecregi,usu_oid,setablero,paciente,entfecingr,enthora,entdiaenfe,entsegrega,entobserva)
                       values(GETDATE(),'".$_SESSION['usuario']."','".$this->tablero."','".$this->paciente."','".$this->fechaIng."','".$this->hora."','".$this->diagnostico."','".$this->segregacion."','".$this->observaciones."')";
            $con->query($insert);
        }  else {
            $update = "update enturenfer
                        set entfecingr = '".$this->fechaIng."',
                            enthora = '".$this->hora."',
                            entdiaenfe = '".$this->diagnostico."',
                            entsegrega = '".$this->segregacion."',
                            entobserva ='".$this->observaciones."'
                       where paciente = '".$this->paciente."' and setablero = '".$this->tablero."'";
            $con->query($update);
        }
         
        $datoshistori = "select enturenfer.oid,entfecregi,enturenfer.usu_oid,setablero,enturenfer.paciente,entfecingr,enthora,cast(entobserva as text)as entobserva,
                         ltrim(rtrim(PACPRINOM+' '+PACSEGNOM+' '+PACPRIAPE+' '+PACSEGAPE)) as NOMBRES,setablero.cama,setablero.servicio
                         from enturenfer 
                         inner join ".$con->getReal()."..GENPACIEN on paciente = PACNUMDOC
                         inner join setablero on setablero.oid = enturenfer.setablero
                         where enturenfer.paciente = '".$this->paciente."' and setablero = '".$this->tablero."'";
        
        $res = $con->query($datoshistori);
        
        while($res2 = mssql_fetch_array($res)){
            
            $oidenturenfer = $res2['oid'];
            $entfecregi = $res2['entfecregi'];
            $usu_oid = $res2['usu_oid'];
            $setablero = $res2['setablero'];
            $paciente = $res2['paciente'];
            $entfecingr = $res2['entfecingr'];
            $enthora = $res2['enthora'];
            // $entdiaenfe = $res2['entdiaenfe'];
            //$entsegrega = $res2['entsegrega'];
            $entobserva = $res2['entobserva'];
            $nombres = $res2['NOMBRES'];
            $cama = $res2['cama'];
            $servicio = $res2['servicio'];
        }
        $insertHitori = "insert into enthistorial(setablero,entdescri,entfechare,usu_oid,enturenfer)
                         values('".$setablero."','Cama: ".$cama.", Servicio: ".$servicio.", 
                                Paciente:".$paciente." - ".$nombres.", Fecha Ingreso: ".$entfecingr.", Hora Ingreso: ".$enthora.", 
                                Observaciones: ".$entobserva.", Usuario Que Realizo el Registro: ".$usu_oid.",
                                Fecha En Que Se Realizo El Registro: ".$entfecregi."',GETDATE(),
                                '".$_SESSION['usuario']."','".$oidenturenfer."')";
        $con->query($insertHitori);
        
        $con->cerrarConexion();
    }
    
    function segregacion(){
        
        include '../../../controlador/php/conexion/Conexion.php';
        $con = new Conexion();
        
        $count = "select COUNT(*) as n from enturenfer where setablero = '".$this->tablero."' and paciente = '".$this->paciente."' ";
        $res = $con->query($count);
        
        while($res2 = mssql_fetch_array($res)){
           $n = $res2['n'];
        }if($n == 0 ){
            $insert = "insert into enturenfer(entfecregi,usu_oid,setablero,paciente,entfecingr,entdiaenfe,entsegrega,entobserva)
                       values('','','".$this->tablero."','".$this->paciente."','','','".$this->segregacion."','')";
            $con->query($insert);
        }  else {
            $update = "update enturenfer
                        set entsegrega = '".$this->segregacion."'
                       where paciente = '".$this->paciente."' and setablero = '".$this->tablero."'";
            $con->query($update);
        }
        
        $con->cerrarConexion();
    }
    
    function datosPac(){
        include '../../../controlador/php/conexion/Conexion.php';
        $con = new Conexion();
        
        $select = "select cast(diagnostico as text)as diagnostico,
                          cast(enfactual as text)as enfactual,
                          cast(tratamiento as text)as tratamiento,
                          cast(observaciones as text)as observaciones,
                          PACPRINOM+' '+PACSEGNOM+' '+PACPRIAPE+' '+PACSEGAPE as nombres
                   from setablero
                   inner join ".$con->getReal()."..GENPACIEN on setablero.paciente = GENPACIEN.PACNUMDOC 
                   where paciente = '".$this->paciente."'";
        $res = $con->query($select);
        
        while($res2 = mssql_fetch_array($res)){
            $lista[] = array_map(utf8_encode,$res2);
        }
        
        print_r(json_encode($lista));
        $con->cerrarConexion();
    }
    
    function alergias(){
        include '../../../controlador/php/conexion/Conexion10.php';
        $con = new Conexion10();
        
        $select = "SELECT TOP 1 HCFECFOL, AINCONSEC, PACNUMDOC, PACPRINOM, PACSEGNOM, PACPRIAPE, PACSEGAPE, 
                    CONVERT(NVARCHAR(5),HCCM09N16) AS '_7', CONVERT(NVARCHAR(500),HCCM04N29) AS '_7cual' 
                    FROM HCMHODETE 
                    INNER JOIN HCNFOLIO ON HCMHODETE.HCNFOLIO = HCNFOLIO.OID 
                    INNER JOIN HCNTIPHIS ON HCNFOLIO.HCNTIPHIS = HCNTIPHIS.OID 
                    INNER JOIN ADNINGRESO ON HCNFOLIO.ADNINGRESO = ADNINGRESO.OID 
                    INNER JOIN GENPACIEN ON ADNINGRESO.GENPACIEN = GENPACIEN.OID 
                    WHERE HCCODIGO='HODETE' AND PACNUMDOC = '".$this->paciente."'
                    ORDER BY HCFECFOL DESC";
        $res = $con->query($select);
        
        while($res2 = mssql_fetch_array($res)){
            $lista[] = array_map(utf8_encode,$res2);
        }
        
        print_r(json_encode($lista));
        $con->cerrarConexion();
        
       
    }
    
    function Imprimir($CodigoServicio){
        include '../../../controlador/php/conexion/Conexion.php';
        $con = new Conexion();
        
        $select = "SELECT t.dolordt,case when t.enfactual is null then '' else cast(t.enfactual as text) end as enfact,
                                 case when t.tratamiento is null then '' else cast(t.tratamiento as text) end as tratamiento,
                                 nes.n,t.oid as oidst,t.oid,t.cama,servicio, paciente,HCACODIGO,dg10.NOMPAC,dg10.AINCONSEC,dg10.GDENOMBRE, 
                                 auxiliar,medico,especialidad,pendientes,t.fecha,cast(diagnostico as text)as diagnostico,cast(observaciones as text)as observaciones,
                                 especialidadoid,clasificacion,atendido,enobservacion,usu_oid,fecharegis,dg10.EDAD,estado,Estados.Nombre
                         FROM setablero as t
                         LEFT OUTER JOIN ".$con->getReal()."..HPNDEFCAM on t.cama = HPNDEFCAM.OID 
                         outer apply(select RTRIM(PACPRINOM+' '+PACSEGNOM+' '+PACPRIAPE+' '+PACSEGAPE)AS NOMPAC, GDENOMBRE,AINCONSEC,SUBSTRING(CONVERT (VARCHAR(10),AINFECING,103),1,10)+' '+CONVERT(VARCHAR(5), AINFECING, 114) AS FECHA,
                         CAST(DATEDIFF(DD, GENPACIEN.GPAFECNAC, GETDATE()) / 365.25 AS INT) AS EDAD
                                     from ".$con->getReal()."..GENPACIEN left outer join ".$con->getReal()."..ADNINGRESO on GENPACIEN.OID = ADNINGRESO.GENPACIEN
                                           INNER JOIN ".$con->getReal()."..GENDETCON AS GE ON ADNINGRESO.GENDETCON=GE.OID
                                     WHERE AINESTADO=0  and PACNUMDOC = t.paciente)as dg10
                          outer apply (select count(*) as n
                                 from setespecialidad
                                 where setespecialidad.setablero = t.oid and setespecialidad.paciente = t.paciente)as nes
                          inner join Cd_Estados_New as Estados on estado = Estados.Oid
                          WHERE servicio = '".$CodigoServicio."'";
        $res = $con->query($select);
        
        while($res2 = mssql_fetch_array($res)){
            $lista[] = array_map(utf8_encode,$res2);
        }
        
        print_r(json_encode($lista));
    }
    
    function Especialidades($Documento){
        include '../../../controlador/php/conexion/Conexion.php';
        $con = new Conexion();
        
        $select = "select GENESPECI.GEEDESCRI,GENESPECI.OID 
                   from setespecialidad left outer join ".$con->getReal()."..GENESPECI on setespecialidad.especialidad = GENESPECI.OID
                   where paciente = '".$Documento."' ";
        $res = $con->query($select);
        
        while($res2 = mssql_fetch_array($res)){
            $lista[] = array_map(utf8_encode,$res2);
        }
        
        print_r(json_encode($lista));
    } 
}


?>
